package Practice;

import com.sun.org.apache.bcel.internal.generic.FMUL;

import java.util.Arrays;

/**
 * create by Sun
 * 2018/8/17
 */
public class FMhuanyuan {
     char val;
     FMhuanyuan cl;
     FMhuanyuan rl;

    public static void main(String[] args) {
        char[] pre="GDAFEMHZ".toCharArray();
        char[] mid="ADEFGHMZ".toCharArray();
        char[] last="AEFDHZMG".toCharArray();
//        System.out.println(Arrays.copyOfRange(pre,1,pre.length));
        FMhuanyuan fMhuanyuan=null;
        //fMhuanyuan=huanyuan(pre,mid);
       fMhuanyuan= MLhuanyuan(mid,last);
        xianxu(fMhuanyuan);
        System.out.println();
       // houxu(fMhuanyuan);

    }

    private static FMhuanyuan MLhuanyuan(char[] mid, char[] last) {
        int length=mid.length;
        if (mid.length==0){
            return null;
        }
        int index=0;
        for(index=0;index<length;index++){
            if(mid[index]==last[last.length-1]){
                break;
            }
        }
        FMhuanyuan fMhuanyuan=new FMhuanyuan();
        fMhuanyuan.val=last[last.length-1];
        fMhuanyuan.cl=MLhuanyuan(Arrays.copyOfRange(mid,0,index),Arrays.copyOfRange(last,0,index));
        fMhuanyuan.rl=MLhuanyuan(Arrays.copyOfRange(mid,index+1,mid.length),Arrays.copyOfRange(last,index,last.length-1));
        return fMhuanyuan;
    }

    private static void xianxu(FMhuanyuan fMhuanyuan) {
        if (fMhuanyuan!=null){
            System.out.print(fMhuanyuan.val+" ");
            xianxu(fMhuanyuan.cl);
            xianxu(fMhuanyuan.rl);

        }
    }
    private static void houxu(FMhuanyuan fMhuanyuan) {
        if (fMhuanyuan!=null){
            houxu(fMhuanyuan.cl);
            houxu(fMhuanyuan.rl);
            System.out.print(fMhuanyuan.val+" ");
        }
    }

    private static FMhuanyuan huanyuan(char[] pre, char[] mid) {
        int length=mid.length;
        if(length==0){
            return null;
        }

        int index=0;
        for(index=0;index<length;index++){
            if(mid[index]==pre[0]){
                break;
            }
        }
        FMhuanyuan fMhuanyuan=new FMhuanyuan();
        fMhuanyuan.val=pre[0];
        fMhuanyuan.cl=huanyuan(Arrays.copyOfRange(pre,1,pre.length),Arrays.copyOfRange(mid,0,index));
        fMhuanyuan.rl=huanyuan(Arrays.copyOfRange(pre,index+1,pre.length),Arrays.copyOfRange(mid,index+1,mid.length));
        return fMhuanyuan;
    }
}
